    package com.xh.bussiness.permission.intercept;

import com.xh.bussiness.permission.service.IUserOnlineService;
import com.xh.bussiness.permission.service.IUserService;
import com.xh.bussiness.permission.util.UserAuthUtil;
import com.xh.bussiness.system.constant.UserOnlineConstant;
import com.xh.core.annotation.ExcludeLoginTokenIntercept;
import com.xh.core.properties.XhProperties;
import com.xh.core.util.StringUtils;
import com.xh.core.util.UserUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.ExecutorService;

/*
 * @Author daiwei
 * @Description
 * @Date 2018/8/16 08:37
 **/
@Slf4j
@Component
public class LoginTokenInterceptor extends HandlerInterceptorAdapter {

    @Autowired
    private IUserService userService;
    @Autowired
    private XhProperties xhProperties;
    @Autowired
    private IUserOnlineService userOnlineService;

    private boolean isAnsynJsonRequest(HttpServletRequest request){
        System.out.println(request.getHeader("x-requested-with"));
        if (request.getHeader("x-requested-with") != null && request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")) { //如果是ajax请求响应头会有x-requested-with
            return true;
        }
        return false;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if(handler.getClass().equals(HandlerMethod.class)&&((HandlerMethod)handler).hasMethodAnnotation(ExcludeLoginTokenIntercept.class)){
            return super.preHandle(request, response, handler);
        }
        String userId=request.getHeader("userId");
        String userToken=request.getHeader("accessToken");
        UserUtils.setCurrentUserId(null);

        Integer os=StringUtils.isEmpty(request.getHeader("os"))?null:Integer.valueOf(request.getHeader("os"));
        Integer system=StringUtils.isEmpty(request.getHeader("system"))?null:Integer.valueOf(request.getHeader("system"));

        if(StringUtils.isEmpty(userId)||StringUtils.isEmpty(userToken)){
            response.setStatus(HttpStatus.FORBIDDEN.value());
            response.setHeader("XH_STATUS", "noLogin");
            return false;
        }else{
            if(xhProperties.isCheckLoginToken()&& !UserAuthUtil.checkToken(os,system,userId,userToken)){
                response.setStatus(HttpStatus.FORBIDDEN.value());
                response.setHeader("XH_STATUS", "noLogin");
                return false;
            }
//            if(UserOnlineConstant.OFFLINE == userOnlineService.newUserOnlineState(userId)){
//                response.setStatus(HttpStatus.FORBIDDEN.value());
//                response.setHeader("XH_STATUS", "noLogin");
//                return false;
//            }
        }
        UserUtils.setCurrentUserId(userId);
        // 更新用户最后一次请求的标识
//        userOnlineService.updateLastRquest(userId, true);
        return super.preHandle(request, response, handler);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object object, Exception exception) throws Exception{
        super.afterCompletion(request,response,object,exception);
    }
}